package com.code.generator.entity;

import org.apache.commons.lang3.StringUtils;


/**
 * 数据库表字段信息
 *
 *
 * @author 朱国军
 * @version 2014年8月25日 朱国军
 */
public class ColumnsEntity {

    private String columnName;

    private String fieldName;

    private String typeName;

    private String javaTypeName;
    /** 字段长度 */
    private int columnSize;

    private String remark;

    private String fullType;

    private boolean isPrimary;

    private String isNullAble;

    private String mothedName;


    /**
     * @return 方法中使用的名称，首字母大写
     */
    public String getMothedName() {
        return mothedName;
    }


    /**
     * @return 是否允许为空（）
     */
    public String getIsNullAble() {
        return isNullAble;
    }


    /**
     * @param isNullAble 要设置的 isNullAble
     */
    public void setIsNullAble(String isNullAble) {
        this.isNullAble = isNullAble;
    }


    /**
     * @return 是否主键
     */
    public boolean getIsPrimary() {
        return isPrimary;
    }


    /**
     * @param isPrimary 是否主键
     */
    public void setIsPrimary(boolean isPrimary) {
        this.isPrimary = isPrimary;
    }


    /**
     * @return java代码中的字段名称
     */
    public String getFieldName() {
        return fieldName;
    }


    /**
     * @return java代码中的字段类型
     */
    public String getJavaTypeName() {
        return javaTypeName;
    }


    /**
     * @return 数据库字段名
     */
    public String getColumnName() {
        return columnName;
    }


    /**
     * @param columnName 数据库字段名
     */
    public void setColumnName(String columnName) {

        this.columnName = columnName;

        String[] words = StringUtils.split(columnName.toLowerCase(), "_");
        StringBuffer sb = new StringBuffer();
        for (String word : words) {
            sb.append(StringUtils.capitalize(word));
        }
        this.mothedName = sb.toString();
        this.fieldName = StringUtils.uncapitalize(sb.toString());
    }


    /**
     * @return 数据库字段类型
     */
    public String getTypeName() {
        return typeName;
    }


    /**
     * @param typeName 数据库字段类型
     */
    public void setTypeName(String typeName) {     
        String dbTypeName = typeName;
        switch (dbTypeName) {
            case "BIGINT": 
                this.javaTypeName = "Long";   
                this.typeName="BIGINT";
                break;
            case "TINYINT":
                this.javaTypeName = "Byte";    
                this.typeName="TINYINT";
                break;
            case "SMALLINT":  
                this.javaTypeName = "Integer";   
                this.typeName="SMALLINT";
                break;
            case "MEDIUMINT":
                this.javaTypeName = "Integer"; 
                this.typeName="INTEGER";
                break;
            case "INTEGER":
                this.javaTypeName = "Integer"; 
                this.typeName="INTEGER";
                break;
            case "INT":
                this.javaTypeName = "Integer"; 
                this.typeName="INTEGER";
                break;
            case "FLOAT":  
                this.javaTypeName = "Float";  
                this.typeName="REAL";
                break;
            case "DOUBLE": 
                this.javaTypeName = "Double";
                this.typeName="DOUBLE";
                break;
            case "DECIMAL":
                this.javaTypeName = "BigDecimal";
                this.fullType = "java.math.BigDecimal";
                this.typeName="DECIMAL";
                break;
            case "NUMERIC":
                this.javaTypeName = "BigDecimal";
                this.fullType = "java.math.BigDecimal";
                this.typeName="DECIMAL";
                break;
            case "CHAR":   
                this.javaTypeName = "String";
                this.typeName="CHAR";
                break;
            case "VARCHAR": 
                this.javaTypeName = "String";
                this.typeName="VARCHAR";
                break;
            case "TINYBLOB":   
                this.javaTypeName = "byte[]";   
                this.typeName="BINARY";
                break;
            case "TINYTEXT":    
                this.javaTypeName = "String"; 
                this.typeName="VARCHAR";
                break;
            case "BLOB":    
                this.javaTypeName = "byte[]";   
                this.typeName="BINARY ";
                break;
            case "TEXT":   
                this.javaTypeName = "String";    
                this.typeName="LONGVARCHAR";
                break;
            case "MEDIUMBLOB":  
                this.javaTypeName = "byte[]";   
                this.typeName="LONGVARBINARY"; 
                break;
            case "MEDIUMTEXT": 
                this.javaTypeName = "String";   
                this.typeName="LONGVARCHAR";
                break;
            case "LONGBLOB":    
                this.javaTypeName = "byte[]";    
                this.typeName="LONGVARBINARY";
                break;
            case "LONGTEXT":    
                this.javaTypeName = "String";    
                this.typeName="LONGVARCHAR";
                break;
            case "DATE":    
                this.javaTypeName = "Date"; 
                this.fullType = "java.util.Date";
                this.typeName="DATE";
                break;
            case "TIME":    
                this.javaTypeName = "Date";
                this.fullType = "java.util.Date";
                this.typeName="TIME";
                break;
            case "YEAR":    
                this.javaTypeName = "Date";  
                this.fullType = "java.util.Date";
                this.typeName="DATE";
                break;
            case "DATETIME":   
                this.javaTypeName = "Date"; 
                this.fullType = "java.util.Date";
                this.typeName="TIMESTAMP";
                break;
            case "TIMESTAMP":  
                this.javaTypeName = "Date"; 
                this.fullType = "java.util.Date";
                this.typeName="TIMESTAMP";
                break;
            default:
                this.javaTypeName = typeName;
        }
    }


    /**
     * @return 数据库字段长度
     */
    public int getColumnSize() {
        return columnSize;
    }


    /**
     * @param columnSize 数据库字段长度
     */
    public void setColumnSize(int columnSize) {
        this.columnSize = columnSize;
    }


    /**
     * @return 数据库字段注释
     */
    public String getRemark() {
        if (remark == null) {
            return "";
        }
        return remark;
    }


    /**
     * @param remark 数据库字段注释
     */
    public void setRemark(String remark) {
        this.remark = remark;
    }


    /**
     * 返回字段类型名全称，用于构造import
     *
     * @return Java字段类型全称，例如：java.sql.Timestamp
     */
    public String getFullType() {
        return fullType;
    }

}
